import { request, qs } from "../service";

const menu = {

  /**
   * 查找菜单列表
   */
  findMenuList(param) {
    return request({
      url: '/auth/menu/findByAttributes',
      method: 'get',
      params: addDefaultParam(param)
    })
  },

  /**
   * 查找菜单总数
   */
  findMenuListMax(param) {
    return request({
      url: '/auth/menu/findMaxByAttributes',
      method: 'get',
      params: addDefaultParam(param)
    })
  },

  /**
   * 添加菜单
   */
  addMenu(param) {
    return request({
      url: '/auth/menu/add',
      method: 'post',
      data: addDefaultParam(param)
    });
  },

  /**
   * 更新菜单
   */
  updateMenu(param) {
    return request({
      url: '/auth/menu/update',
      method: 'put',
      data: addDefaultParam(param)
    });
  },

  /**
   * 删除菜单
   */
  delMenu(menuId) {
    return request({
      url: '/auth/menu/delete/' + menuId,
      method: 'delete'
    });
  },

  /**
   * 批量删除菜单
   */
   delBatchMenu(param) {
    return request({
      url: `/auth/menu/deleteBatch`,
      method: 'delete',
      params: param,
      paramsSerializer: params => {
        return qs.stringify(params, {
          indices: false
        })
      }
    })
  },

  /**
   * 获取用户菜单
   */
   getUserMenus(){
    return request({
      url: '/auth/menu/getUserMenus',
      method: 'get',
      params: addMenuParam({})
    })
   },

  /**
   * 获取用户路由
   */
      getUserRoutes(){
      return request({
        url: '/auth/menu/getUserRoutes',
        method: 'get',
        params: addMenuParam({})
      })
    },

  /**
   * @description 导入系统菜单
   * @param {Object} params
  */
  import(params) {
    params.append("day", -1)
    return request({
      url: `/auth/menu/import`,
      method: 'post',
      headers: { 'Content-Type': 'multipart/form-data' },
      data: params
    })
  },

  /**
    * 导出系统菜单
    */
  export(param){
    return request({
      url: '/auth/menu/export',
      method: 'get',
      responseType:'blob',
      params: addMenuParam(param)
    })
  }
}

/**
 * 添加缺省参数值
 * @param {*} param 原始参数
 * @returns 携带缺省参数返回
 */
function addDefaultParam(param) {
  let params = param;
  params['app'] = process.env.VUE_APP_SYSTEM_NAME;
  return params;
}

/**
 * 添加用户菜单默认参数
 * @param {*} param 
 * @returns 
 */
function addMenuParam(param) {
  let params = param;
  params['app'] = process.env.VUE_APP_SYSTEM_NAME;
  params['type'] = process.env.VUE_APP_MENU_TYPE;
  return params;
}

export default menu;
